﻿var frxs = {
    //获取url中的参数
    getUrlParam: function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg); //匹配目标参数
        if (r != null) return decodeURIComponent(r[2]);
        return null; //返回参数值
    },
    //数字输入左带0不删除
    onlymath: function() {
        $(".onlymath").keypress(function() {
            return (/[\d.]/.test(String.fromCharCode(event.keyCode)));
        });
        $(".onlymath").blur(function () {
            if (isNaN($(this).val())) {
                var value = "";
                var array = $(this).val().split('');
                for (var i = 0; i < array.length; i++) {
                    if (isNaN(array[i])) {
                        break;
                    } else {
                        value += array[i];
                    }
                }
                $(this).val(value);
            }
        });
    },
    //数字 只能输入正整数
    onlyint: function () {
        $(".onlyint").keypress(function (evt) {
            evt = (evt) ? evt : ((window.event) ? window.event : "");
            var key = evt.keyCode ? evt.keyCode : evt.which;
            return (/[\d]/.test(String.fromCharCode(key))) || (key == 46) || (key == 8);
        });
        $(".onlyint").blur(function () {
            var value = $(this).val();
            if(!value||isNaN(value)) {
                value = 1;
            }
            $(this).val(parseInt(value));
        });
    },
    //数字 保留两位小数
    onlyfloat: function () {
        $(".onlyfloat").keypress(function (evt) {
            evt = (evt) ? evt : ((window.event) ? window.event : "");
            var key = evt.keyCode ? evt.keyCode : evt.which;
            return (/[\d.]/.test(String.fromCharCode(key)));
        });
        $(".onlyfloat").blur(function () {
            var value = $(this).val();
            if (!value || isNaN(value) || value<=0) {
                value = 1.00;
            }
            $(this).val(parseFloat(value).toFixed(2));
        });
    },
    //必填验证
    validateForm: function () {
        var isvalidate = true;
        $(".onnull").each(function () {
            var val = $.trim($(this).val());
            if(!val) {
                isvalidate = false;
                $(this).css("border", "1px solid red");
                return isvalidate;
            }
        });
        return isvalidate;
    },
    //得到当前时间时分秒-不传参数为默认格式
    dateTimeFormat: function (value,formatter) {
        var date = new Date(value.replace(/-/g,'/'));
        var year = date.getFullYear();
        var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : +("0") + (date.getMonth() + 1);
        var day = date.getDate() > 9 ? date.getDate() : ("0" + date.getDate());
        var hours = date.getHours() > 9 ? date.getHours() : ("0" + date.getHours());
        var minutes = date.getMinutes() > 9 ? date.getMinutes() : ("0" + date.getMinutes());
        var seconds = date.getSeconds() > 9 ? date.getSeconds() : ("0" + date.getSeconds());
        var dateStr = year + "-" + month + "-" + day + " " + hours + ":" + minutes;
        if (formatter) {
            formatter = formatter.replace(/yyyy/g, year);
            formatter = formatter.replace(/YYYY/g, year);
            formatter = formatter.replace(/MM/g, month);
            formatter = formatter.replace(/dd/g, day);
            formatter = formatter.replace(/DD/g, day);
            formatter = formatter.replace(/hh/g, hours);
            formatter = formatter.replace(/HH/g, hours);
            formatter = formatter.replace(/mm/g, minutes);
            formatter = formatter.replace(/ss/g, seconds);
            formatter = formatter.replace(/SS/g, seconds);
            dateStr = formatter;
        }
        return dateStr;
    },
    //替换过滤斜杠和双引号
    filterText:function (value) {
        return value.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
    }
    
};


$(function() {
    //初始化
    frxs.onlymath();
    frxs.onlyint();
    frxs.onlyfloat();
});

//重写toFixed方法  
Number.prototype.toFixed = function (d) {
    var s = this + "";
    if (!d) d = 0;
    if (s.indexOf(".") == -1) s += ".";
    s += new Array(d + 1).join("0");
    if (new RegExp("^(-|\\+)?(\\d+(\\.\\d{0," + (d + 1) + "})?)\\d*$").test(s)) {
        var s = "0" + RegExp.$2, pm = RegExp.$1, a = RegExp.$3.length, b = true;
        if (a == d + 2) {
            a = s.match(/\d/g);
            if (parseInt(a[a.length - 1]) > 4) {
                for (var i = a.length - 2; i >= 0; i--) {
                    a[i] = parseInt(a[i]) + 1;
                    if (a[i] == 10) {
                        a[i] = 0;
                        b = i != 1;
                    } else break;
                }
            }
            s = a.join("").replace(new RegExp("(\\d+)(\\d{" + d + "})\\d$"), "$1.$2");

        }
        if (b) s = s.substr(1);
        return (pm + s).replace(/\.$/, "");
    }
    return this + "";

};
